package farmacia.dao;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import conexao.banco.CriaConexao;




import farmacia.arvore.Arvore;
import farmacia.entidade.estoque.Substancia;

public class SubstanciaDao {
	List<Substancia>substancia ;
	
	private Connection conexao;

    public SubstanciaDao() throws SQLException {
        this.conexao = CriaConexao.getConexao();

    }
             // Metodo de inserção em banco de dados mysql
    public void adicionar(Substancia c1) throws SQLException {
        String sql = "insert  into tb_substancias("
                + "fi_substancia) values(?)";
        
        PreparedStatement stmt = conexao.prepareStatement(sql);

        stmt.setString(1, c1.getSubstancia());
        
        stmt.executeUpdate();
        stmt.close();

    }

    public void excluir(Substancia c1) throws SQLException {
        
        String sql = "delete from tb_substancias where fi_pk_id= ?";
        
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        stmt.setInt(1, c1.getFi_id());
        
        stmt.execute();
        stmt.close();
    }

    public void alterar(Substancia c1) throws SQLException {
        String sql = "UPDATE tb_substancias SET "
                + "nome=?"
                +"WHERE fi_pk_id=?";
        
        PreparedStatement stmt = conexao.prepareStatement(sql);
        
                  // setar valores
           
        stmt.setString(1, c1.getSubstancia());
        stmt.setInt(11, c1.getFi_id());
        
        
        stmt.executeUpdate();
        stmt.close();

    }

    
    
    
    
    public List<Substancia> getLista(String nome) throws SQLException{
        
    String sql = "select * from tb_substancias where nome like ? ORDER BY nome ASC";
    PreparedStatement stmt = this.conexao.prepareStatement(sql);
    stmt.setString(1, nome);
    ResultSet rs = stmt.executeQuery();
    
   
    List <Substancia> listaClientes = new ArrayList<Substancia>();
    
    while (rs.next()){
     
    	Substancia c1 = new Substancia();
                c1.setFi_id(Integer.valueOf(rs.getString("fi_pk_id")));
                c1.setSubstancia(rs.getString("fi_substancia"));
                
                 
        listaClientes.add(c1);
    }    
    rs.close();
    stmt.close();
   
    return listaClientes;
     
    }
    
    
    public ArrayList<Substancia> getLista() throws SQLException{
        
    	String sql = "select fi_substancia from tb_substancias";  
        PreparedStatement stmt = this.conexao.prepareStatement(sql);  
        ResultSet rs = stmt.executeQuery();  
  
        List<Substancia> minhalista = new ArrayList<Substancia>();  
  
        while (rs.next()){  
  
            Substancia cad = new Substancia();  
            cad.setSubstancia(rs.getString("fi_substancia"));//(rs.getString("nome"));  
            minhalista.add(cad);  
        }  
        rs.close();  
        stmt.close();  
    
        return (ArrayList<Substancia>) minhalista;  
    }

}
